package cn.dnuihyy406.dnuifood_16110100406.controller;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.UserModel;
import okhttp3.internal.DiskLruCache;

public class RegisterActivity extends BaseActivity implements OnRetrofitListener<ResultBean>
        ,View.OnClickListener{
    private EditText E_Username,E_Password,E_Phonenum,E_Adress,E_Content;
    private Button RRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    void initViews() {
        layout_file = R.layout.activity_register;
        setLayout(layout_file);
        E_Username = findViewById(R.id.user_name);
        E_Password = findViewById(R.id.pass_word);
        E_Phonenum = findViewById(R.id.phone_number);
        E_Adress = findViewById(R.id.ad_ress);
        E_Content = findViewById(R.id.con_tent);
        RRegister = findViewById(R.id.b_register);

    }

    @Override
    void initEvents() {
        RRegister.setOnClickListener(this);

    }

    @Override
    void initData() {

    }



    @Override
    public void onSuccess(ResultBean object,int flag) {
        if (object.getSuccess().equals("1")){
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(RegisterActivity.this,msg,Toast.LENGTH_SHORT).show();
    }




    @Override
    public void onClick(View view) {
        if (E_Username.getText().toString().trim().equals("")||E_Password.getText().toString().trim().equals("")||
                E_Phonenum.getText().toString().trim().equals("")||E_Adress.getText().toString().trim().equals("")
                ||E_Content.getText().toString().trim().equals("")){
            Toast.makeText(RegisterActivity.this,"各项不能空",Toast.LENGTH_SHORT).show();
        }
        else
        {
            reg();
        }
    }

    public void reg(){
        UserModel model = new UserModel();
        model.reg(E_Username.getText().toString(),E_Password.getText().toString(),
                E_Phonenum.getText().toString(),E_Adress.getText().toString(),
                E_Content.getText().toString(),this);
    }
}
